﻿using Keras.Models;
using Numpy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KerasNetHelper
{
    /// <summary>
    /// 输入层参数值
    /// </summary>
    public class InputParam
    {
        /// <summary>
        /// 参数列表
        /// </summary>
        private List<double> VariableList { set; get; }

        /// <summary>
        /// 预测值
        /// </summary>
        public List<double> PredictValue { set; get; }

        /// <summary>
        /// 检索值
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public double this[int index]
        {
             get
            {
                return this.VariableList[index];
            }
        }

        /// <summary>
        /// 数据点数目
        /// </summary>
        public int Count
        {
            get
            {
                return this.VariableList.Count;
            }
        }

        /// <summary>
        /// 添加值
        /// </summary>
        /// <param name="value"></param>
        public void Add(double value)
        {
            this.VariableList.Add(value);
        }

        /// <summary>
        /// 转为数组
        /// </summary>
        /// <returns></returns>
        public double[] GetArray()
        {
            return this.VariableList.ToArray();
        }

        /// <summary>
        /// 转为多维数组
        /// </summary>
        /// <returns></returns>
        public NDarray<double> GetNDsArray()
        {
            //初始化
            double[,] dataArry = new double[1, this.Count];
            var array = this.GetArray();
            //遍历
            for (int subIndex = 0; subIndex < array.Length; subIndex++)
                dataArry[0, subIndex] = array[subIndex];
            return np.array(dataArry);
        }

        /// <summary>
        /// 预测结果
        /// </summary>
        /// <param name="baseModel"></param>
        /// <returns></returns>
        public List<double> Predict(BaseModel baseModel)
        {
            //预测结果
            var value = baseModel.Predict(this.GetNDsArray());
            //获得所有结果
            var predictValues = value.GetData<float>().ToList().ConvertAll(f => (double)f);
            //获得值
            this.PredictValue = predictValues;
            //获得所有数据
            return this.PredictValue;
        }

        /// <summary>
        /// 预测结果
        /// </summary>
        /// <param name="baseModel"></param>
        /// <returns></returns>
        public List<double> Predict()
        {
            //创建神经网络
            var baseModel = KerasModel.GetBaseModel();
            //神经网络是否存在
            if (baseModel == null) return new List<double> ();
            //预测结果
            return this.Predict(baseModel);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public InputParam()
        {
            this.VariableList = new List<double>();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="variableNum"></param>
        public InputParam(int variableNum)
        {
            this.VariableList = new List<double>(variableNum);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="variableList"></param>
        public InputParam(List<double> variableList)
        {
            this.VariableList = variableList;
        }
    }
}
